﻿Imports DevExpress.XtraBars.Helpers
Imports BUS
Imports DTO
Imports System.IO
Public Class FormMain

    Public _daDN As Integer = 0
    Public Shared _formNV As New FormQLNhanVien()
    Public Shared _formCTruc As New FormQLCaTruc()
    Public _banBUS As New BanBUS()

    '------Thêm/ Sửa------
    Public _loaiTK As Integer = 0
    Public _ctQuyenBUS As New ChiTietQuyenBUS()
    Public _tenDN As String
    '------Thêm/ Sửa------

    Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RibbonControlMain.SelectedPage = RibbonPageQLChucNangChinh

        SetChucNang()

        DevExpress.UserSkins.OfficeSkins.Register()
        DevExpress.UserSkins.BonusSkins.Register()
        SkinHelper.InitSkinPopupMenu(BarSubItemSkin)
        SkinHelper.InitSkinGallery(rgitemSkin)

        DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = SkinHelper.GetSkinCaptionBySkinName("Money Twins")

    End Sub


    Public Function IsLoadForm(ByVal formname As String) As Boolean
        Dim kiemtra As Boolean = False
        For i As Integer = 0 To Application.OpenForms.Count - 1
            If TypeName(Application.OpenForms(i)).ToUpper = formname.ToUpper Then
                kiemtra = True
            End If
        Next

        Return kiemtra
    End Function

    Private Sub BarButtonItemDSBanBia_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemDSBanBida.ItemClick
        Dim _formQLBan As New FormQLBan()

        If (IsValidForm(_formQLBan.Name) = False) Then
            _formQLBan.MdiParent = Me
            _formQLBan._loaiTK = _loaiTK
            _formQLBan.Show()
        End If
    End Sub


    Private Sub BarButtonItem3_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemMinimize.ItemClick
        Me.WindowState = WindowState.Minimized
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Application.Exit()
    End Sub

    'Kiểm tra form đã có chưa nếu có rồi trả ra true, chưa có thì trả ra false
    Public Function IsValidForm(ByVal nameAddTab) As Boolean
        For i = 0 To Me.MdiChildren.Length - 1
            If (Me.MdiChildren(i).Name = nameAddTab) Then
                Me.MdiChildren(i).Focus()
                Return True
            End If
        Next
        Return False
    End Function


    Private Sub bbitemDSNhanVien_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemDSNhanVien.ItemClick
        Dim _formNV As New FormQLNhanVien()
        If (IsValidForm(_formNV.Name) = False) Then
            _formNV.MdiParent = Me
            _formNV.Show()
        End If
    End Sub

    Private Sub bbitemQLCaTruc_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemQLCaTruc.ItemClick
        Dim _formcTruc As New FormQLCaTruc
        If (IsValidForm(_formcTruc.Name) = False) Then
            _formcTruc.MdiParent = Me
            _formcTruc.Show()
        End If
    End Sub

    Private Sub bbitemDangNhap_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemDangNhap.ItemClick
        Me.Visible = False
        Dim _formDN As New FormDangNhap()
        _formDN.ShowDialog()
        Me.Visible = True        
        If (_formDN._flag = 1) Then
            _daDN = 1

            '------Thêm/ Sửa------
            _tenDN = _formDN._tenTK
            '------Thêm/ Sửa------

            _loaiTK = _formDN._loaiTK
            SetChucNang()
        End If

    End Sub

    '------Thêm/ Sửa------
    Public Sub SetChucNang()
        If (_daDN = 0) Then
            bbitemDangNhap.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
            bbitemDangXuat.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
            bbitemDoiMK.Enabled = False
        Else
            bbitemDoiMK.Enabled = True
            bbitemDangNhap.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
            bbitemDangXuat.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
        End If
        If (_loaiTK = 0) Then
            'Tài khoản thường
            'Quản lý bàn
            bbitemDSBanBida.Enabled = False
            bbitemThemBan.Enabled = False
            bbitemXoaBan.Enabled = False
            bbitemSuaBan.Enabled = False
            'Quản lý hoá đơn
            bbitemQLHoaDon.Enabled = False
            'Quản lý nhân viên
            bbitemDSNhanVien.Enabled = False
            'Quản lý ca trực
            bbitemQLCaTruc.Enabled = False
            'Thống kê và báo cáo
            'Doanh thu
            bbitemDoanhThuThang.Enabled = False
            bbitemDoanhThuQuy.Enabled = False
            bbitemDoanhThuNam.Enabled = False
            'Doanh thu
            bbitemLuongToanNV.Enabled = False
            bbitemLuongTungNV.Enabled = False
            'Tài khoản
            bbitemDangKy.Enabled = False
            bbitemPhanQuyen.Enabled = False
        Else
            If (_loaiTK = 1) Then ' Loại tài khoản là admin
                bbitemPhanQuyen.Enabled = True
            End If
            Dim _listQuyen As New List(Of ChiTietQuyenDTO)
            _listQuyen = _ctQuyenBUS.LayDSCTQuyenTuMaLoaiTKBUS(_loaiTK)
            For i = 0 To RibbonControlMain.Items.Count - 1
                For Each _ctQuyenDTO As ChiTietQuyenDTO In _listQuyen
                    If (RibbonControlMain.Items(i).Name.Contains(_ctQuyenDTO.Quyen)) Then
                        RibbonControlMain.Items(i).Enabled = True
                    End If
                Next
            Next
        End If

    End Sub
    '------Thêm/ Sửa------

    Private Sub bbitemThemBan_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemThemBan.ItemClick
        Dim _formThemBan As New FormThemBan()
        _formThemBan.ShowDialog()
        If (_formThemBan._flag = 1) Then
            Dim _loai As Integer
            If (_formThemBan.cbLoaiBan.SelectedItem.ToString().Contains("1")) Then
                _loai = 1
            ElseIf (_formThemBan.cbLoaiBan.SelectedItem.ToString().Contains("2")) Then
                _loai = 2
            ElseIf (_formThemBan.cbLoaiBan.SelectedItem.ToString().Contains("3")) Then
                _loai = 3
            End If
            _banBUS.ThemBanBUS(_loai)
            CapNhatFormBan()
        End If

    End Sub

    Private Sub bbitemSuaBan_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemSuaBan.ItemClick
        Dim _formSuaBan As New FormSuaBan()
        _formSuaBan.ShowDialog()
        If (_formSuaBan._flag = 1) Then
            Dim _loai As Integer
            If (_formSuaBan.rbBanThuong.Checked = True) Then
                _loai = 1
            ElseIf (_formSuaBan.rbBanBidaLo.Checked = True) Then
                _loai = 2
            ElseIf (_formSuaBan.rbBanCarom.Checked = True) Then
                _loai = 3
            End If
            _banBUS.SuaBanBUS(Integer.Parse(_formSuaBan.cbMaBan.SelectedItem.ToString()), _loai)
            CapNhatFormBan()
        End If

    End Sub

    Public Sub CapNhatFormBan()
        For i = 0 To Me.MdiChildren.Length - 1
            If (Me.MdiChildren(i).Name = "FormQLBan") Then
                CType(Me.MdiChildren(i), FormQLBan).LoadDSBanGUI()
                Me.MdiChildren(i).Focus()
            End If
        Next
    End Sub

    Private Sub bbitemXoaBan_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemXoaBan.ItemClick

        Dim _formXoaBan As New FormXoaBan()
        _formXoaBan.ShowDialog()
        If (_formXoaBan._flag = 1) Then
            _banBUS.XoaBanBUS(_formXoaBan.cbMaBan.SelectedItem.ToString())
            CapNhatFormBan()
        End If
    End Sub

    Private Sub FormMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing        
        
    End Sub

    Public Function WriteTextFile(ByVal _pathFile As String, ByVal _contentText As String)
        Dim strFile As String = _pathFile
        Dim sw As StreamWriter
        Try
            If (Not File.Exists(strFile)) Then
                sw = File.CreateText(strFile)
                sw.WriteLine(_contentText)
            Else
                sw = File.AppendText(strFile)
            End If
            sw.WriteLine(_contentText)
            sw.Close()
        Catch ex As IOException
            MsgBox("Error writing to log file.")
        End Try
    End Function

    Private Sub bbitemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemThoat.ItemClick        
        Application.Exit()
    End Sub

    '------Thêm/ Sửa------
    Private Sub bbitemDangXuat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemDangXuat.ItemClick
        _daDN = 0
        _loaiTK = 0
        MessageBox.Show("Thoát tài khoản thành công")
        SetChucNang()
    End Sub
    '------Thêm/ Sửa------

    Private Sub bbitemDoiMK_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemDoiMK.ItemClick
        Dim _formDoiMK As New FormDoiMKTaiKhoan()
        _formDoiMK._tenDN = _tenDN
        _formDoiMK.ShowDialog()
    End Sub

    Private Sub bbitemPhanQuyen_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemPhanQuyen.ItemClick
        Dim _formPhanQuyen As New FormPhanQuyen()
        _formPhanQuyen.ShowDialog()
    End Sub

    Private Sub bbitemLuong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemLuong.ItemClick
        Dim frm As New FormLuong()
        If (IsValidForm(frm.Name) = False) Then
            frm.MdiParent = Me
            frm.Show()
        End If
    End Sub

    Private Sub bbitemQLThucDon_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbitemQLThucDon.ItemClick
        Dim frm As New FormThucDon()
        If (IsValidForm(frm.Name) = False) Then
            frm.MdiParent = Me
            frm.Show()
        End If
    End Sub
End Class
